package com.kh.first.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.kh.first.config.WxCpProperties;
import com.kh.first.config.WxCpProperties.AppConfig;
import com.kh.first.pojo.WfWxShareEntity;
import org.apache.commons.lang3.StringUtils;

public class Tools {
//	public static WfWxShareEntity accessToken = new WfWxShareEntity();
//	public static final WxCpProperties properties = new WxCpProperties();
//	public static final WxCpProperties.AppConfig appConfig= new WxCpProperties.AppConfig();
//	public static final List<AppConfig> appConfigList=new ArrayList<WxCpProperties.AppConfig>();
//	static {
//		properties.setCorpId("wwf6b5c6eaa5ca098d");
//		appConfig.setAgentId(1000004);
//		appConfig.setSecret("s47-M-ZxbwHi-hqwjgwKTDeThP2o5CMPeY0Vo5Y09_w");
//		appConfig.setToken("z4om8tT1w");
//		appConfig.setAesKey("OqQU8nCOw2fQJKMTdEuR5AB4hSQN2GybES2zSa3ehlh");
//		List<WxCpProperties.AppConfig> list = new ArrayList<>();
//		list.add(appConfig);
//		properties.setAppConfigs(list);
//	}
	public static Long getRandom() {
		SimpleDateFormat dateformat2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = dateformat2.format(new Date());
		String randomString = s.substring(0, 10) + new java.util.Random().nextInt(100000000);
		return Long.parseLong(randomString);
	}

	/*
	 * 生成六位随机整数
	 */
	public static String getSixRandomNum() {
		String s = "";
		for (int i = 0; i < 6; i++) {
			int n;
			n = (int) (Math.random() * 10);
			s = s + n;
		}
		return s;
	}

	public static Float roundDown(Float source) {
		BigDecimal result = new BigDecimal(source);
		result = result.setScale(2, BigDecimal.ROUND_HALF_UP);
		Float f = result.floatValue();
		return f;
	}
	public static String getYear() {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy");
		return dateformat.format(new Date());
	}

	public static void main(String[] args) {
		System.out.println(roundDown(15.8000001907349f));
	}

	public static String getIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	public static String getRequestPath(HttpServletRequest request) {
		String requestPath = "";
		if (StringUtils.isNotEmpty(request.getQueryString())) {
			requestPath = request.getRequestURI() + "?" + request.getQueryString();
			if (requestPath.indexOf("&") > -1) {// 去掉其他参数
				requestPath = requestPath.substring(0, requestPath.indexOf("&"));
			}
			requestPath = requestPath.substring(request.getContextPath().length() + 1);// 去掉项目路径
		} else {
			requestPath = request.getRequestURI();
			requestPath = requestPath.substring(request.getContextPath().length() + 1);// 去掉项目路径
		}

		return requestPath;
	}

	static ObjectMapper mapper = new ObjectMapper();
	static{
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		mapper.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE);
	}

	public static String toJson(Object json) throws JsonProcessingException {
		return mapper.writeValueAsString(json);
	}
}
